package com.tt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tt.entity.pojo.MovieCommentPO;
import com.tt.entity.pojo.MovieInfoPO;
import com.tt.entity.pojo.MovieInfoTypePO;
import com.tt.service.MovieCommentService;
import com.tt.service.MovieInfoCommentService;
import com.tt.service.MovieInfoService;
import com.tt.service.MovieInfoTypeService;
import com.tt.util.ConstUtil;
import org.apache.logging.log4j.util.Strings;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 电影信息表 前端控制器
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:11
 */
@RestController
public class MovieInfoController {

    private final MovieInfoService movieInfoServiceImpl;
    private final MovieInfoCommentService movieInfoCommentServiceImpl;

    private final MovieCommentService movieCommentServiceImpl;
    private final MovieInfoTypeService movieInfoTypeServiceImpl;

    public MovieInfoController(MovieInfoService movieInfoServiceImpl, MovieInfoCommentService movieInfoCommentServiceImpl, MovieCommentService movieCommentServiceImpl, MovieInfoTypeService movieInfoTypeServiceImpl) {
        this.movieInfoServiceImpl = movieInfoServiceImpl;
        this.movieInfoCommentServiceImpl = movieInfoCommentServiceImpl;
        this.movieCommentServiceImpl = movieCommentServiceImpl;
        this.movieInfoTypeServiceImpl = movieInfoTypeServiceImpl;
    }


    /**
     * 根据id获取电影
     *
     * @param movieId
     * @return
     */
    @GetMapping("/movie/{movieId}")
    public MovieInfoPO getMovieInfo(@PathVariable("movieId") Integer movieId) {
        if (movieId <= 0) {
            return null;
        }
        return movieInfoServiceImpl.getById(movieId);
    }

//    /**
//     * 根据电影id获取评论
//     */
//    @GetMapping("/comment/{movieId}")
//    @Deprecated
//    public ArrayList<UserCommentVO> getMovieComments(@PathVariable("movieId") Integer movieId) {
//        if (movieId == null || movieId <= 0) {
//            return null;
//        }
////        return movieInfoCommentServiceImpl.getMovieComments(movieId);
//        return null; // 此方法不用. 因为不需要用户表了, 用户评论和用户信息绑定到评论表中
//    }

    @GetMapping("/comment/{movieId}")
    public List<MovieCommentPO> getMovieComments(@PathVariable("movieId") Integer movieId, Integer current) throws UnsupportedEncodingException {
        if (movieId == null || movieId <= 0 || current == null) {
            return null;
        }

        Page<MovieCommentPO> iPage = new Page<>(current, ConstUtil.PAGE_SIZE);
        Page<MovieCommentPO> comments = movieCommentServiceImpl.page(iPage, new QueryWrapper<MovieCommentPO>().eq("movie_id", movieId));
        List<MovieCommentPO> records = comments.getRecords();
        for (int i = 0; i < records.size(); i++) {
            if (!StringUtils.isEmpty(records.get(i).getName())) {
                // 出库后，使用 Base64 解码用户名
                String username = new String(Base64.decodeBase64(records.get(i).getName().getBytes()), "utf-8");
                records.get(i).setName(username);
                //                records.set(i, records.get(i).setName(username));
            }

        }
        return records;
    }

    /**
     * 返回总页数
     */
    @GetMapping("/total")
    public Long getTypePages(String type) {

        if (Strings.isEmpty(type)) {
            return 0L;
        }

        if ("0".equals(type)) {
            return movieInfoServiceImpl.count();
        }
        return movieInfoTypeServiceImpl.count(new QueryWrapper<MovieInfoTypePO>().eq("type", type));
    }
}
